package com.log.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Log;
import com.common.untils.CommonResult;
import com.log.mapper.LogMapper;
import com.log.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-02
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CommonResult insertLog(String caozuo,String token) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        Log log = new Log();
        log.setUname(user.getString("name"));
        log.setAction(caozuo);
        logMapper.insert(log);
        return new CommonResult(2000,"添加日志成功",null);
    }

    @Override
    public CommonResult findAllLog(Integer currentPage,Integer pagesize,Log log) {
        Page<Log> page= new Page<>(currentPage,pagesize);
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.hasText(log.getUname())){
            wrapper.like("uname",log.getUname());
        }
        Page<Log> page1 = logMapper.selectPage(page, wrapper);
        return new CommonResult(2000,"查询成功" ,page1);
    }
}
